﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace HDJ.Framework.Core {
    public class ConsoleManager : AppModuleBase
    {
        private bool isOpenConsol = false;
        private ConsoleWindow consoleWindow;
        private const string ConsoleWindowFrefabName = "ConsoleWindow";

        public override string GetModuleName()
        {
            return typeof(ConsoleManager).Name;
        }

        public override string GetModuleVersion()
        {
            return "0.0.1";
        }
        public override void OnUpdate()
        {
            if (Input.touchCount > 5)
            {
                isOpenConsol = !isOpenConsol;
                OpenWindow(isOpenConsol);
            }
            if (Input.GetKeyDown(KeyCode.F12))
            {
                isOpenConsol = !isOpenConsol;
                OpenWindow(isOpenConsol);
            }
        }

        private void OpenWindow(bool isOpen)
        {
            if (consoleWindow == null)
            {
                GameObject obj = Resources.Load<GameObject>(ConsoleWindowFrefabName);
                obj =GameObject.Instantiate(obj);
                obj.name = "[ConsoleWindow]";
                consoleWindow = obj.GetComponent<ConsoleWindow>();

                EventSystem eventSystem = GameObject.FindObjectOfType<EventSystem>();
                if (eventSystem == null)
                {
                    GameObject temp = new GameObject("EventSystem");
                    temp.AddComponent<EventSystem>();
                    temp.AddComponent<StandaloneInputModule>();
                }
            }
            if (isOpen)
            {
                consoleWindow.Open();
            }
            else
            {
                consoleWindow.Close();
            }
        }

  
    }
}
